package com.zhuoshi.serviceImpl;

import java.util.Collection;
import java.util.List;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhuoshi.bean.WdHtRoom;
import com.zhuoshi.mapper.WdHtRoomMapper;

import net.sf.json.JSONArray;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Service
public class RedisOpera
{
	    @Autowired
	    private  JedisPool jedisPool;//注入JedisPool
	    
	    @Autowired
	    private  WdHtRoomMapper wdHtRoomMapper;//注入JedisPool

	    //插入到redis 中   注意  修改  添加 删除信息的时候必须要调用此方法进行redis 的数据更新
	    public  void insertIntoRedis()
	    {
	        //获取ShardedJedis对象
	        Jedis jedis = jedisPool.getResource();
	        
	        jedis.flushAll();
	        
	    	List<WdHtRoom> list = wdHtRoomMapper.selectAll(null);
	    	if(list!=null&&list.size()>0)
	    	{
	    		JSONArray jsonArray = JSONArray.fromObject(list);
		         //存入键值对
		        jedis.set("rooms", jsonArray.toString());
	    	}
	         //回收ShardedJedis实例
	        jedis.close();
	        
	    }
	    
	  //查询redis中的所有的信息
	    public  List<WdHtRoom> listFromRedis()
	    {
	    	 //获取ShardedJedis对象
	        Jedis jedis = jedisPool.getResource();
	        //查询到信息
	        String string = jedis.get("rooms");
	        System.out.println(string+"===========");
	        JSONArray fromObject = JSONArray.fromObject(string);
	        List<WdHtRoom> list =  (List<WdHtRoom>)JSONArray.toCollection(fromObject, WdHtRoom.class);
	        
	         //回收ShardedJedis实例
	        jedis.close();
	        
			return list;
	    }
}
